using System;
using System.Collections.Generic;
using System.Windows;
namespace MicroMsg.UI.UserContrl
{
	public class UIActionService
	{
		private const string TAG = "UIActionService";
		private UIElement mEle;
		private List<UIAction> RefreshItems = new List<UIAction>();
		private object RefreshArrayLock = new object();
		private RoutedEventHandler itemLoaded;
		private List<UILoadAction> LoadItems = new List<UILoadAction>();
		private object LoadArrayLock = new object();
		private object ImgRefreshArrayLock = new object();
		private Dictionary<object, UIAction> ImgRefreshDic = new Dictionary<object, UIAction>();
		public event RoutedEventHandler LoadedComplete;
		public UIActionService(UIElement uielement_0)
		{
			this.mEle = uielement_0;
			this.itemLoaded = new RoutedEventHandler(this.item_Loaded);
		}
		public void Push(UIAction action)
		{
			if (action == null)
			{
				return;
			}
			lock (this.RefreshArrayLock)
			{
				this.RefreshItems.Add(action);
			}
			if (this.RefreshItems.get_Count() <= 1)
			{
				this.Pop();
			}
		}
		public void Cancel()
		{
			lock (this.RefreshArrayLock)
			{
				this.RefreshItems.Clear();
			}
		}
		private void Pop()
		{
			if (this.RefreshItems.get_Count() <= 0)
			{
				return;
			}
			this.mEle.get_Dispatcher().BeginInvoke(delegate
			{
				lock (this.RefreshArrayLock)
				{
					if (this.RefreshItems.get_Count() <= 0)
					{
						return;
					}
					UIAction uIAction = this.RefreshItems.get_Item(0);
					uIAction();
					this.RefreshItems.RemoveAt(0);
				}
				this.Pop();
			});
		}
		public void PushLoadAction(UILoadAction loadAction)
		{
			if (loadAction == null)
			{
				return;
			}
			lock (this.LoadArrayLock)
			{
				this.LoadItems.Add(loadAction);
			}
			if (this.LoadItems.get_Count() <= 1)
			{
				this.PopLoadAction();
			}
		}
		private void PopLoadAction()
		{
			if (this.LoadItems.get_Count() <= 0)
			{
				if (this.LoadedComplete != null)
				{
					this.LoadedComplete.Invoke(null, null);
				}
				return;
			}
			this.mEle.get_Dispatcher().BeginInvoke(delegate
			{
				lock (this.LoadArrayLock)
				{
					if (this.LoadItems.get_Count() <= 0)
					{
						if (this.LoadedComplete != null)
						{
							this.LoadedComplete.Invoke(null, null);
						}
					}
					else
					{
						UILoadAction uILoadAction = this.LoadItems.get_Item(0);
						uILoadAction(new RoutedEventHandler(this.item_Loaded));
						this.LoadItems.RemoveAt(0);
					}
				}
			});
		}
		private void item_Loaded(object sender, RoutedEventArgs e)
		{
			this.PopLoadAction();
		}
		public void PushImgRefresh(object keyObj, UIAction action)
		{
			if (action == null)
			{
				return;
			}
			if (keyObj == null)
			{
				return;
			}
			lock (this.ImgRefreshArrayLock)
			{
				this.ImgRefreshDic.set_Item(keyObj, action);
			}
			if (this.ImgRefreshDic.get_Count() <= 1)
			{
				this.PopImgRefresh();
			}
		}
		public void CancelImgRefresh()
		{
			lock (this.ImgRefreshArrayLock)
			{
				this.ImgRefreshDic.Clear();
			}
		}
		private void PopImgRefresh()
		{
			if (this.ImgRefreshDic.get_Count() <= 0)
			{
				return;
			}
			this.mEle.get_Dispatcher().BeginInvoke(delegate
			{
				lock (this.ImgRefreshArrayLock)
				{
					if (this.ImgRefreshDic.get_Count() <= 0)
					{
						return;
					}
					using (Dictionary<object, UIAction>.Enumerator enumerator = this.ImgRefreshDic.GetEnumerator())
					{
						if (enumerator.MoveNext())
						{
							KeyValuePair<object, UIAction> current = enumerator.get_Current();
							UIAction value = current.get_Value();
							value();
							this.ImgRefreshDic.Remove(current.get_Key());
						}
					}
				}
				this.PopImgRefresh();
			});
		}
	}
}
